// pages/home/home.js
import {byeHome, getUserLogin} from '../../apis/api'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
		bgPic: '',
    nickName: '',
    exitBtnIsShow: false,
    isLog: 0,
    token: ''
  },
	getUserProfile: function() {
		wx.getUserProfile({
			desc: '用于完善会员资料',
			success: (res) => {
				let user = res.userInfo;
				app.globalData.userInfo = res.userInfo;
				wx.setStorageSync('user', user)
				this.setData({
						isShowUserName: true,
						userInfo: user,
            bgPic: res.userInfo.avatarUrl,
            nickName: res.userInfo.nickName
				}); 
			},
			fail: res => {
				console.log("获取用户信息失败", res)
			}
		 })
  },
  exitLogin: async function() {
    const token = wx.getStorageSync('token')
    await byeHome(token)
    wx.clearStorageSync();
    this.setData({
      exitBtnIsShow: false
    })
    wx.showToast({
      title: '登出成功',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      token: wx.getStorageSync('token'),
    })
    this.getLog()
  },
  getLog: function () {
    if (!!!wx.getStorageSync('token')) wx.setStorageSync('token', 'token null')
    getUserLogin().then(res => {
      if (res.code === 200) {
        this.setData({
          isLog: res.data
        })
        if(this.data.token && this.data.isLog){
          this.setData({
            exitBtnIsShow: true
          })
        }else {
          this.setData({
            exitBtnIsShow: false
          })
        }
        console.log(this.data.exitBtnIsShow);
      }
    })
  },
  toLogin() {
    wx.navigateTo({
      url: '/pages/tomeslog/tomeslog',
    })
  },

  onShow: function () {
    this.setData({
      token: wx.getStorageSync('token'),
    })
    this.getLog()
  }
})